package com.example.obs.controller;

import com.example.obs.model.domin.Department;
import com.example.obs.model.domin.ObsFile;
import com.example.obs.model.utils.ObsPage;
import com.example.obs.service.DepartmentService.DepartmentService;
import com.example.obs.service.FlileService.FileService;
import com.example.obs.service.ObjectService.BucketObjectService;
import com.example.obs.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Api(tags = "部门接口")
@RequestMapping("/departments")
@RestController
//@PreAuthorize("isAuthenticated()")
public class DepartmentsController {
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private FileService fileService;
    @Autowired
    private BucketObjectService bucketObjectService;
    /**
     * 查询部门列表
     * @param department
     * @return
     */
    @ApiOperation(value = "查询部门列表")
    @RequestMapping(value = "/selectDepartmentList",method = RequestMethod.POST )
    public ObsPage selectDepartmentList(@RequestBody Department department){
        ObsPage departments = new ObsPage();
        List<Department> list = departmentService.ListDepartment();
        departments.setData(list);
        departments.setTotal(departments.getData().size());
        System.out.println(list.get(0).getAdmins());
        return  departments;
    }

    /**
     * 添加部门
     * @param department
     * @return
     */
    @ApiOperation(value = "添加部门",notes ="添加部门信息并创建部门文件夹")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public int addDepartment(@RequestBody Department department){
        return departmentService.addDepartment(department);
    }

    /**
     * 修改部门名称
     * @param department
     * @return
     */
    @ApiOperation(value = "修改部门名称")
    @RequestMapping(value = "/changeName",method = RequestMethod.POST)
    public int changeName(@RequestBody Department department) throws IOException {
        if (departmentService.updateName(department)==0){
            return 2;
        }
        if (fileService.Exist(0,"部门/"+department.getDepartmentName())){
            bucketObjectService.newFolder("部门/"+department.getDepartmentName());
            ObsFile obsFile = new ObsFile();
            obsFile.setFileState(1);
            obsFile.setUserId(0);
            obsFile.setFileName(department.getDepartmentName());
            obsFile.setPath("部门/"+department.getDepartmentName());
            obsFile.setType("文件夹");
            fileService.addFile(obsFile);
        }
        return 1;
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @ApiOperation(value = "删除部门",notes = "但不删除部门的文件")
    @RequestMapping(value = "/deleteDepartment",method = RequestMethod.POST)
    public int deleteDepartment(@RequestBody Integer id){
        departmentService.deleteDepartment(id);
        return 1;
    }

    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public String test(){
        List<Department> list = departmentService.ListDepartment();
       return list.toString();
    }

}
